package com.newer.weatherforecast.sms;

import java.util.Date;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

import com.newer.weatherforecast.dao.ConfigDAO;
import com.newer.weatherforecast.entity.MyConfig;

public class SmsBroadCastReceiver extends BroadcastReceiver {
	public static final String PARAM_DEST = "DEST";
	public static final String PARAM_MSG = "MSG";
	public void onReceive(Context context, Intent intent) {
		System.out.println("收到信息");
		ConfigDAO configDAO = new ConfigDAO(context);
		MyConfig myConfig = configDAO.QueryMyConfig();
		Date date = new Date();
		String time = date.toGMTString();
		if(!myConfig.isAllow_sms()){
			return;
		}
		Bundle bundle = intent.getExtras();
		Object[] pdus = (Object[]) bundle.get("pdus");
		SmsMessage[] smsMsg = new SmsMessage[pdus.length];
		String from = "";
		String text = "";

		for (int i = 0; i < smsMsg.length; i++) {
			smsMsg[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
			from = smsMsg[i].getDisplayOriginatingAddress();
			text = smsMsg[i].getDisplayMessageBody();
		}
		Intent intentService = new Intent("com.newer.weatherforecast.smsservice");
		intentService.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intentService.putExtra(PARAM_DEST, from);
		intentService.putExtra(PARAM_MSG, text);
		intentService.putExtra("time", time);
		intentService.putExtra("myConfig", myConfig);
		context.startService(intentService);
	}
}
